<?php

// 多次通知控制器

namespace app\api\controller;

use app\api\controller\Base;
use think\facade\Cache;
use think\facade\Log;
use think\Db;

class FailNotify extends Base
{

    /**
     * 多次通知
     */
    public function query()
    {
        try {
            // 只允许命令行访问
            if (PHP_SAPI != 'cli') {
                return ['code' => 999, 'msg' => '404'];
            }

            // 设置多少秒通知一次
            $notice_time = 15;

            $map[] = ['merchant_id', '<>', 12345678];
            $map[] = ['count', '<', 10];
            $map[] = ['status', '=', 0];
            $map[] = ['manual', '=', 0];

            Db::name('payment_order_notify')
                ->where($map)
                ->chunk(10, function ($all) use ($notice_time) {
                    foreach ($all as $single) {

                        // 通知规则
                        if ($single['update_time'] + $notice_time < time()) {

                            $redis = Cache::store('redis');
                            // 可能会在一秒中多次通知相同订单，用redis处理。
                            $order_notify_key = 'order_notify_' . $single['order_id'];
                            $order_notify = $redis->has($order_notify_key);
                            if ($order_notify) {
                                //payment_error_log(760001, $single, '多次通知:在一秒中多次通知相同订单');
                                unset($single);
                                continue;
                            } else {
                                $redis->set($order_notify_key, 1, 5);
                            }

                            $resdata = go_curl($single['url'], '', 'get', 12);
                            // 通知成功
                            if (stripos($resdata, 'success') !== false) {
                                try {
                                    Db::name('payment_order_notify')
                                        ->where('order_id', $single['order_id'])
                                        ->inc('count')
                                        ->update(['status' => 1, 'update_time' => time()]);
                                } catch (\Exception $e) {
                                    payment_error_log(760002, $single, $e->getMessage());
                                }
                            } else {

                                $msg_title = '';
                                switch ($single['trade_type']) {
                                    case 'consumption':
                                        $msg_title = '支付';
                                        break;
                                    case 'paid':
                                        $msg_title = '代付';
                                        break;
                                    default:
                                        $msg_title = '';
                                        break;
                                }

                                try {
                                    Db::name('payment_order_notify')
                                        ->where('order_id', $single['order_id'])
                                        ->inc('count')
                                        ->update(['update_time' => time(), 'remark' => substr($resdata, 0, 60000)]);
                                } catch (\Exception $e) {
                                    payment_error_log(760003, $single, $e->getMessage());
                                }

                                $count = $single['count'] + 1;
                                Log::record("{$msg_title}通知商户号{$single['merchant_id']}失败记录：" . $single['order_id'] . '——' . $count, 'log');
                                Log::record("{$msg_title}通知URL参数：" . $single['url'], 'log');
                                Log::record("{$msg_title}通知返回内容：" . $resdata, 'log');
                            }
                        }

                        unset($single);
                    }
                }, 'update_time', 'asc');
        } catch (\Exception $e) {
            payment_error_log(760000, $e->getMessage());
            return ['code' => 999, 'msg' => '404'];
        }
    }
}
